package com.s64.dao;

import com.s64.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class SysUserDaoImpl implements SysUserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int add(SysUser sysUser) {
        return jdbcTemplate.update("INSERT INTO `t_sys_user` (`account`, `realName`, " +
                "`sex`, `birthday`, `phone`, `address`, `roleId`) VALUES " +
                "(?,?,?,?,?,?,?)", sysUser.getAccount(), sysUser.getRealName(), sysUser.getSex(),
                sysUser.getBirthday(), sysUser.getPhone(), sysUser.getAddress(), sysUser.getRoleId());
    }

    @Override
    public int del(Integer id) {
        return jdbcTemplate.update("delete from t_sys_user where id = ?", id);
    }

    @Override
    public int update(SysUser sysUser) {
        return jdbcTemplate.update("update `t_sys_user` set `account`=?, `realName`=?, " +
                        "`sex`=?, `birthday`=?, `phone`=?, `address`=?, `roleId`=? where id = ?", sysUser.getAccount(), sysUser.getRealName(), sysUser.getSex(),
                sysUser.getBirthday(), sysUser.getPhone(), sysUser.getAddress(), sysUser.getRoleId(), sysUser.getId());
    }

    @Override
    public SysUser get(Integer id) {
        return jdbcTemplate.queryForObject("select * from t_sys_user where id = ?", new BeanPropertyRowMapper<>(SysUser.class), id);
    }

    @Override
    public List<SysUser> getAll() {
        return jdbcTemplate.query("select * from t_sys_user", new BeanPropertyRowMapper<>(SysUser.class));
    }
}
